package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.enums.FacilityInfoTypeEnum;
import com.atguigu.lease.web.admin.mapper.FacilityInfoMapper;
import com.atguigu.lease.web.admin.service.FacilityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.FacilityInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 1
* @description 针对表【facility_info(配套信息表)】的数据库操作Service实现
* @createDate 2024-08-28 11:00:26
*/
@Service
public class FacilityInfoServiceImpl extends ServiceImpl<FacilityInfoMapper, FacilityInfo>
    implements FacilityInfoService {
    @Override
    public List<FacilityInfo> listByType(FacilityInfoTypeEnum typeEnum) {
        LambdaQueryWrapper<FacilityInfo> queryWrapper = null;
        if(typeEnum!=null){
            queryWrapper = Wrappers.lambdaQuery(FacilityInfo.class)
                    .eq(FacilityInfo::getType , typeEnum.getCode());
        }

        return this.list(queryWrapper);
    }
    //新增或更新配套信息业务
    @Override
    public boolean saveOrUpdateFacilityInfo(FacilityInfo facilityInfo) {
        //唯一校验：  type +  name组合唯一
        long id = facilityInfo.getId()==null?-1L:facilityInfo.getId();
        FacilityInfo dbFacilityInfo = this.getOne(Wrappers.lambdaQuery(FacilityInfo.class)
                .eq(FacilityInfo::getType, facilityInfo.getType().getCode())
                .eq(FacilityInfo::getName, facilityInfo.getName()));
        if(dbFacilityInfo!=null && id != dbFacilityInfo.getId().longValue()){
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }

        return this.saveOrUpdate(facilityInfo);
    }
    /*
        抽取业务代码：
            1、提高controller的可读性
            2、业务层方法更方便aop编程
     */
//    @Override
//    public List<FacilityInfo> listByType(Integer type) {
//        LambdaQueryWrapper<FacilityInfo> queryWrapper = null;
//        if(type!=null){
//            queryWrapper = Wrappers.lambdaQuery(FacilityInfo.class)
//                    .eq(FacilityInfo::getType , type);
//        }
//
//        return this.list(queryWrapper);
//    }
}




